package com.rustic.server.controller.admapi;

import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.annotation.RepeatSubmit;
import cn.xy.commons.core.Result;
import cn.xy.commons.enums.YesNoStatus;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.dto.user.UpdateIsLockDTO;
import com.rustic.server.dto.user.UpdatePwdDTO;
import com.rustic.server.dto.user.UserUpdateDTO;
import com.rustic.server.po.User;
import com.rustic.server.query.UserAdminListQuery;
import com.rustic.server.service.UserService;
import com.rustic.server.vo.user.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 表[user]对应的后台管理类
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/admapi/user")
@Api(tags = {"用户管理接口"})
public class UserAdminController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户-列表")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('user-list')")
    public Result<Page<UserListVO>> list(UserAdminListQuery userQuery) {
        return Result.data(userService.getPageList(userQuery));
    }

    @ApiOperation(value = "用户-详情")
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('user-detail')")
    public Result<User> detail(int id) {
        if (id <= 0) {
            return Result.error(UserService.USER_NOT_EXIST);
        }
        User user = userService.getById(id);
        if (user == null) {
            return Result.error(UserService.USER_NOT_EXIST);
        }
        return Result.data(user);
    }

    @ApiOperation(value = "用户-更新")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('user-update')")
    @OperateLog(description = "更新用户")
    @RepeatSubmit
    public Result<String> update(@RequestBody @Validated UserUpdateDTO userUpdateDTO) {
        userUpdateDTO.valid();
        userService.updateUser(userUpdateDTO);
        return Result.ok("修改成功");
    }

    @ApiOperation(value = "用户-禁用启动")
    @PostMapping("/disable")
    @PreAuthorize("@validate.hasPermission('user-disable')")
    @OperateLog(description = "禁用/启用用户")
    @RepeatSubmit
    public Result<String> disable(@Validated @RequestBody UpdateIsLockDTO updateIsLockDTO) {
        userService.updateStatusBatchById(updateIsLockDTO.getIds(), updateIsLockDTO.getIsLock());
        String result = YesNoStatus.isNo(updateIsLockDTO.getIsLock()) ? "启用成功" : "禁用成功";
        return Result.ok(result);
    }

    @ApiOperation(value = "用户-修改密码")
    @PutMapping("/updatePassword")
    @PreAuthorize("@validate.hasPermission('user-update-password')")
    @OperateLog(description = "修改密码")
    @RepeatSubmit
    public Result<String> updatePassword(@Validated @RequestBody UpdatePwdDTO updatePwdDTO) {
        User user = userService.getById(updatePwdDTO.getId());
        if (user == null) {
            return Result.error(UserService.USER_NOT_EXIST);
        }
        userService.updatePassword(user.getId(), updatePwdDTO.getPassword());
        return Result.ok("修改成功");
    }
}
